package com.net.oil.user.fragment;

import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseFragment;
import com.net.oil.bean.ListDate;
import com.net.oil.user.adapter.MyReceiveGoodsRecyclerViewAdapter;
import com.net.oil.user.contract.MyReceiveGoodsContract;
import com.net.oil.user.presenter.MyReceiveGoodsPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyReceiveGoodsListFragment extends BaseFragment<MyReceiveGoodsPresenter> implements MyReceiveGoodsContract.View {

    @BindView(R.id.goods_recycler_view)
    RecyclerView goodsRecyclerView;
    Unbinder unbinder;
    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;
    private int used = -2;          //使用状态(1:已使用 0：未使用 -1：已过期)

    private MyReceiveGoodsRecyclerViewAdapter adapter;

    public static MyReceiveGoodsListFragment newInstance(int position) {
        MyReceiveGoodsListFragment fragment = new MyReceiveGoodsListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.MY_RECEIVE_GOODS_POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            int position = bundle.getInt(Constant.MY_RECEIVE_GOODS_POSITION, -1);
            if (position == 0) {
                used = 0;
            }
            if (position == 1) {
                used = 1;
            }
            if (position == 2) {
                used = -1;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_receive_goods_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        presenter = new MyReceiveGoodsPresenter(this);

        initView();
        initData();
        return view;
    }

    private void initData() {
        if (used != -2){
            showProgressDialog();
            presenter.getMyReceiveGoods(used, page, rows);
        }
    }

    private void initView() {
        adapter = new MyReceiveGoodsRecyclerViewAdapter(getContext(), used);
        goodsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        goodsRecyclerView.setAdapter(adapter);

        goodsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!recyclerView.canScrollVertically(1) && haveMore && page > 1) {
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getMyReceiveGoods(used, page, rows);
                }
            }
        });
    }


    /**
     * 获取我领取的商品成功
     *
     * @param myReceiveGoodsBean 商品数据
     */
    @Override
    public void getMyReceiveGoodsSuccess(ListDate myReceiveGoodsBean) {
        dismissProgressDialog();
        if (myReceiveGoodsBean != null){
            if (myReceiveGoodsBean.getList() != null && myReceiveGoodsBean.getList().size() > 0){
                if (page == 1){
                    adapter.updateData(myReceiveGoodsBean.getList());
                }else {
                    adapter.loadMore(myReceiveGoodsBean.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }
    }

    /**
     * 获取我领取的商品失败
     */
    @Override
    public void getMyReceiveGoodsFailed() {
        dismissProgressDialog();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
